<template>
  <el-container class="main_container">
    <!-- 头部布局 -->
    <el-header>
      <!-- logo和项目名称 -->
      <div class="left_box">
        <img src="../assets/img/avatar.png" alt />
        <span>We管理系统</span>
      </div>
      <!-- 用户登录展示的头像 -->
      <div class="right_box">
        <el-dropdown>
          <img src="../assets/img/avatar.png" alt />
          <!-- 下拉菜单 -->
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item icon="el-icon-house">系统首页</el-dropdown-item>
            <el-dropdown-item icon="el-icon-ship">交流讨论</el-dropdown-item>
            <el-dropdown-item icon="el-icon-switch-button">退出登录</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </div>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse?'60px':'200px'">
        <!-- 展开/收起 -->
        <div class="toggle_box" @click="toggleCollapse">|||</div>
        <!-- 菜单容器 -->
        <el-menu
          :default-active="activePath"
          class="el-menu-vertical-demo"
          background-color="#001529"
          text-color="#fff"
          active-text-color="#ffd04b"
          :collapse="isCollapse"
          :collapse-transition="false"
          :unique-opened="true"
          router
        >
          <!-- 菜单内容 -->
          <MenuTree :menuList="this.menuList"></MenuTree>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <!-- 路由视图 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
// 导入组件
import MenuTree from "../components/MenuTree";
import {getMenu} from "../api/menu";
export default{
  name: "Main",
  data() {
    return {
      isCollapse: false,
      activePath: "",
      menuList:[]
    };
  },
  //  注册组件
  components: {
    MenuTree
  },
  created(){
    this.getMenu();
  },
  methods: {
    // 控制左侧栏开关
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    // 从后台接受菜单数据
    async getMenu(){
      const { data } = await getMenu();
      this.menuList = data.data.data;
    },
  }
};
</script>

<style lang="scss" scpoed>
.main_container {
  height: 100%;
}
/* 下拉菜单的样式 */
.el-dropdown-link {
  cursor: pointer;
  color: #409eff;
}
.el-icon-arrow-down {
  font-size: 12px;
}
// 头部样式
.el-header {
  background-color: #001529;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  padding-left: 0;
  color: #ffffff;
  // 左边logo和标题
  .left_box {
    display: flex;
    align-items: center;

    // logo
    img {
      width: 60px;
      height: 60px;
      margin: 0px 0px 10px 15px;
    }

    // 标题
    span {
      margin-left: 15px;
    }
  }
  // 右边登陆头像
  .right_box {
    .el-dropdown {
      & > img {
        height: 60px;
        width: 60px;
        border-radius: 50%;
        background-color: #ffffff;
        margin: 0 15px 0 0;
      }
    }
  }
}
// 侧边栏
.el-aside {
  background-color: #001529;
  color: #333;
  .el-menu {
    border-right: none;
  }
  // 展开/收起
  .toggle_box {
    background-color: cadetblue;
    font-size: 15px;
    font-weight: bold;
    line-height: 25px;
    color: #ffffff;
    letter-spacing: 0.2em;
    text-align: center;
    cursor: pointer;
  }
}

// 主要内容
.el-main {
  background-color: #e9eef3;
}
</style>
